/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trabalhocompiladores.View;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;
import trabalhocompiladores.controle.controleFrameView;

/**
 *
 * @author u031254
 */
public class FrameView extends javax.swing.JFrame {

    /**
     * Creates new form FrameView
     */
    private controleFrameView controle = new controleFrameView();
    private JFileChooser chooser = new JFileChooser();
    private JTable tabela =  new JTable();

    public FrameView() {
        initComponents();
        
        jscrollpane.setViewportView(tabela);
        tabela.setLocation(0, 0);
        tabela.setSize(200, 200);
        tabela.setVisible(false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textFieldExpressao = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButtonSave = new javax.swing.JButton();
        jButtonLoad = new javax.swing.JButton();
        jButtonGerarAFN = new javax.swing.JButton();
        jscrollpane = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        textFieldExpressao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldExpressaoActionPerformed(evt);
            }
        });

        jLabel1.setText("String:");

        jButtonSave.setText("Save");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonLoad.setText("Load");
        jButtonLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadActionPerformed(evt);
            }
        });

        jButtonGerarAFN.setText("Gerar");
        jButtonGerarAFN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGerarAFNActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jButtonSave)
                        .addGap(59, 59, 59)
                        .addComponent(jButtonLoad))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jscrollpane, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textFieldExpressao))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonGerarAFN)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textFieldExpressao, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButtonGerarAFN))
                .addGap(29, 29, 29)
                .addComponent(jscrollpane, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonLoad))
                .addGap(41, 41, 41))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGerarAFNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGerarAFNActionPerformed
        tabela.setModel(controle.gerarAFN(textFieldExpressao.getText()));
        tabela.setVisible(true);
    }//GEN-LAST:event_jButtonGerarAFNActionPerformed

    private void textFieldExpressaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldExpressaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldExpressaoActionPerformed

    /*
     Ao clicar no botão de salvar, sistema deve abrir um file chooser e selecionar onde o usuario vai salvar seus dados.
     * no formato XML
     */
    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed


        //chooser.setCurrentDirectory(new java.io.File(".xml"));
        chooser.setDialogTitle("Escolha o diretório para salvar o arquivo");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("xml files (*.xml)", "xml");
        chooser.setFileFilter(xmlfilter);
        //   
        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            controle.salvarAutomato(chooser.getSelectedFile());
        }
    }//GEN-LAST:event_jButtonSaveActionPerformed
    /*
     Ao clicar no botão de load, sistema deve abrir um file chooser e selecionar o arquivo xml para carregar os dados.
     * no formato XML
     */

    private void jButtonLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadActionPerformed

        chooser.setDialogTitle("Escolha o arquivo");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("xml files (*.xml)", "xml");
        chooser.setFileFilter(xmlfilter);
        //   
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            tabela.setModel(controle.carregarAutomato(chooser.getSelectedFile()));
            tabela.setVisible(true);
        }

    }//GEN-LAST:event_jButtonLoadActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGerarAFN;
    private javax.swing.JButton jButtonLoad;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jscrollpane;
    private javax.swing.JTextField textFieldExpressao;
    // End of variables declaration//GEN-END:variables
}
